import tkinter as tk
from user_info import UserInfo
from user_session import UserSession

class MainWindow:
    def __init__(self, main):
        # 获取登录账号
        login_name = UserSession.get_attribute("login_name")
        user = UserSession.get_attribute(login_name)
        
        # 设置主窗口
        main.title("学生成绩管理系统")
        main.geometry("600x400")
        main.minsize(600, 400)
        main.configure(bg="#F0F0F0")
        
        # 设置窗口位置居中
        screen_width = main.winfo_screenwidth()
        screen_height = main.winfo_screenheight()
        x = (screen_width - 600) // 2
        y = (screen_height - 400) // 2
        main.geometry(f"600x400+{x}+{y}")
        
        # 创建主框架
        main_frame = tk.Frame(main, bg="#F0F0F0")
        main_frame.place(relx=0.5, rely=0.5, anchor="center")
        
        # 欢迎信息
        welcome_label = tk.Label(main_frame, 
                               text=f"{login_name}，您好！欢迎进入学生成绩管理系统！",
                               font=("华文行楷", 18, "bold"),
                               fg="#2E8B57",
                               bg="#F0F0F0")
        welcome_label.grid(row=0, column=0, columnspan=2, pady=(0, 20))
        
        # 学生信息管理标题
        info_title = tk.Label(main_frame,
                            text="学生信息管理",
                            font=("华文行楷", 16, "bold"),
                            fg="#2E8B57",
                            bg="#F0F0F0")
        info_title.grid(row=1, column=0, columnspan=2, pady=(0, 15))
        
        # 创建信息显示框架
        info_frame = tk.Frame(main_frame, bg="#E8E8E8", relief="flat")
        info_frame.grid(row=2, column=0, columnspan=2, padx=20, pady=10)
        
        # 设置信息框架的网格权重
        info_frame.grid_columnconfigure(0, weight=1)
        info_frame.grid_columnconfigure(1, weight=1)
        
        # 第一行信息 - 编号和姓名
        id_frame = tk.Frame(info_frame, bg="#E8E8E8", highlightthickness=0)
        id_frame.grid(row=0, column=0, padx=30, pady=20, sticky="ew")
        
        id_label = tk.Label(id_frame, 
                          text="编号：",
                          font=("微软雅黑", 14),
                          bg="#E8E8E8")
        id_label.pack(side="left", padx=(0, 15))
        id_value = tk.Label(id_frame,
                          text=f"{user.id_no}",
                          font=("微软雅黑", 14),
                          bg="#E8E8E8")
        id_value.pack(side="left")
        
        name_frame = tk.Frame(info_frame, bg="#E8E8E8", highlightthickness=0)
        name_frame.grid(row=0, column=1, padx=30, pady=20, sticky="ew")
        
        name_label = tk.Label(name_frame,
                            text="姓名：",
                            font=("微软雅黑", 14),
                            bg="#E8E8E8")
        name_label.pack(side="left", padx=(0, 15))
        name_value = tk.Label(name_frame,
                            text=f"{user.name}",
                            font=("微软雅黑", 14),
                            bg="#E8E8E8")
        name_value.pack(side="left")
        
        # 第二行信息 - 年龄和性别
        age_frame = tk.Frame(info_frame, bg="#E8E8E8", highlightthickness=0)
        age_frame.grid(row=1, column=0, padx=30, pady=20, sticky="ew")
        
        age_label = tk.Label(age_frame,
                           text="年龄：",
                           font=("微软雅黑", 14),
                           bg="#E8E8E8")
        age_label.pack(side="left", padx=(0, 15))
        age_value = tk.Label(age_frame,
                           text=f"{user.age}",
                           font=("微软雅黑", 14),
                           bg="#E8E8E8")
        age_value.pack(side="left")
        
        sex_frame = tk.Frame(info_frame, bg="#E8E8E8", highlightthickness=0)
        sex_frame.grid(row=1, column=1, padx=30, pady=20, sticky="ew")
        
        sex_label = tk.Label(sex_frame,
                           text="性别：",
                           font=("微软雅黑", 14),
                           bg="#E8E8E8")
        sex_label.pack(side="left", padx=(0, 15))
        sex_value = tk.Label(sex_frame,
                           text=f"{user.sex}",
                           font=("微软雅黑", 14),
                           bg="#E8E8E8")
        sex_value.pack(side="left")
        
        # 设置主框架的网格权重
        main_frame.grid_columnconfigure(0, weight=1)
        main_frame.grid_columnconfigure(1, weight=1)
        
        # 使窗口可调整大小
        main.grid_rowconfigure(0, weight=1)
        main.grid_columnconfigure(0, weight=1)

